package com.jsu.springboot_medicine.controller;

import com.jsu.springboot_medicine.common.Result;
import com.jsu.springboot_medicine.common.StatusCode;
import com.jsu.springboot_medicine.entity.Hospital;
import com.jsu.springboot_medicine.entity.User;
import com.jsu.springboot_medicine.service.HospitalService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author js
 * @name HospitalController
 * @Date 2023/10/1 12:58
 */
@RestController
@RequestMapping("/hospital")
@CrossOrigin
public class HospitalController {
    @Resource
    private HospitalService hospitalService;

    @GetMapping
    public Result<List<Hospital>> selectAll() {
        List<Hospital> hospitals = hospitalService.selectAll();
        return new Result<List<Hospital>>(true, StatusCode.OK, "查询成功", hospitals);
    }

    @GetMapping("/{id}")
    public Result<Hospital> selectById(@PathVariable("id") int id){
        Hospital hospital = hospitalService.selectById(id);
        if (hospital==null){
            Hospital hospital1=new Hospital();
            hospital1.setUserId(id);
            hospitalService.insert(hospital1);
            return new Result<Hospital>(true,StatusCode.OK,"查询成功",hospital1);
        }
        return new Result<Hospital>(true,StatusCode.OK,"查询成功",hospital);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Hospital hospital) {
        hospitalService.update(hospital);
        return new Result(true, StatusCode.OK, "修改成功",null);
    }

    @GetMapping("/selectByName/{name}")
    public Result<List<Hospital>> selectByName(@PathVariable("name")String name) {
        List<Hospital> hospitals = hospitalService.selectByName(name);
        return new Result<List<Hospital>>(true, StatusCode.OK, "查询成功", hospitals);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") int id) {
        hospitalService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功",null);
    }

    @PostMapping("/inset")
    public Result insert(@RequestBody Hospital hospital) {
        hospitalService.insert(hospital);
        return new Result(true, StatusCode.OK, "插入成功",null);
    }

    @GetMapping("/count/{name}")
    public Result countByNameLike(@PathVariable("name") String name){
        int i = hospitalService.countByNameLike(name);
        return new Result(true,StatusCode.OK,"查询成功",i);
    }
}
